{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8d70e8fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": "\ntry {\nrequire(['notebook/js/codecell'], function(codecell) {\n  codecell.CodeCell.options_default.highlight_modes[\n      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n      Jupyter.notebook.get_cells().map(function(cell){\n          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n  });\n});\n} catch (e) {};\n"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": "\ntry {\nrequire(['notebook/js/codecell'], function(codecell) {\n  codecell.CodeCell.options_default.highlight_modes[\n      'magic_text/x-csrc'] = {'reg':[/^%%pybind11/]};\n  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n      Jupyter.notebook.get_cells().map(function(cell){\n          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n  });\n});\n} catch (e) {};\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import numpy as np\n",
    "import pynq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d17a0bc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "bitfile = \"im2col_conv.bit\"\n",
    "overlay = pynq.Overlay(bitfile)\n",
    "# !./dfs 0 200\n",
    "dma = overlay.axi_dma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82683eaa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e9f02b07",
   "metadata": {},
   "outputs": [],
   "source": [
    "height, width, in_channels = 5, 5, 4\n",
    "out_channels = 1\n",
    "P_ICH, P_OCH = 4, 1\n",
    "kernel_size, stride, padding = 3, 1, 1\n",
    "A_BIT, W_BIT, B_BIT = 8, 8, 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c6c43419",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_size = height * width * in_channels\n",
    "input_data = (np.arange(input_size) % 128).astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0cc5148d",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_buffer = pynq.allocate(shape=(in_channels * height * width), dtype=np.uint8, cacheable=1)\n",
    "output_buffer = pynq.allocate(shape=(out_channels * height * width), dtype=np.uint32, cacheable=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "af2634e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.copyto(input_buffer, input_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "392f5481",
   "metadata": {},
   "outputs": [],
   "source": [
    "dma.sendchannel.transfer(input_buffer)\n",
    "dma.recvchannel.transfer(output_buffer)\n",
    "dma.recvchannel.wait()\n",
    "dma.sendchannel.wait()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fd0ec34b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PynqBuffer([ 6552, 10468, 12724, 14980,  9816, 14964, 22254, 24774, 27294,\n",
       "            17172, 24324, 34854, 37374, 39894, 24612, 33684, 47454, 49974,\n",
       "            52494, 32052, 16920, 22564, 23668, 24772, 14040], dtype=uint32)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_buffer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b3aae6f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
